package com.wyvoid.forum.web.action.forum;

import com.wyvoid.forum.domain.Post;
import com.wyvoid.forum.domain.SessionUser;
import com.wyvoid.forum.domain.Topic;
import com.wyvoid.forum.service.IPostService;
import com.wyvoid.forum.service.ITopicService;
import com.wyvoid.forum.web.action.BaseAction;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.Date;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-26
 * Time: 18:30:18
 */
@UrlBinding(value = "/post.do")
public class PostAction extends BaseAction {
    @SpringBean
    private ITopicService topicService;
    @SpringBean
    private IPostService postService;
    private long tid;
    private Post post;
    private long pid;
    private long qutoeId;

    @DefaultHandler
    public Resolution postList() {
        return getBBSResolution("postList");
    }

    public void setTid(long tid) {
        this.tid = tid;
    }

    public void setTopicService(ITopicService topicService) {
        this.topicService = topicService;
    }


    public void setPostService(IPostService postService) {
        this.postService = postService;
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    public void setPid(long pid) {
        this.pid = pid;
    }

    public void setQutoeId(long qutoeId) {
        this.qutoeId = qutoeId;
    }

    public Resolution newThreadForm() {
        return getBBSResolution("postForm");
    }

    public Resolution newThread() {
        Date now = new Date();
        SessionUser sessionUser = context.getUser();

        Topic topic = new Topic();
        topic.setTitle(post.getTitle());
        topic.setCreateTime(now);
        topic.setForumId(post.getForumId());
        topic.setIp(context.getRequest().getRemoteAddr());

        if (sessionUser != null) {
            topic.setAuthorId(sessionUser.getId());
            topic.setAuthorName(sessionUser.getNickname());
        }

        post.setCreateTime(now);
        post.setFirst(true);
        post.setIp(context.getRequest().getRemoteAddr());
        post.setLastUpdateTime(now);

        if (sessionUser != null) {
            post.setAuthorId(sessionUser.getId());
            post.setAuthorName(sessionUser.getNickname());
        }

        postService.newTopic(topic, post);


        return new RedirectResolution(ForumAction.class).addParameter("forumId", post.getForumId());
    }

    public Resolution postForm() {
        if (tid > 0) {
            Topic topic = topicService.getTopic(tid);
            post=new Post();
            post.setTitle(topic.getTitle());
            post.setTopicId(topic.getId());
        } else {
            return newThreadForm();
        }
        return getBBSResolution("postForm");
    }

    public Resolution doPost() {
        Date now = new Date();
        SessionUser sessionUser = context.getUser();
        post.setCreateTime(now);
        post.setIp(context.getRequest().getRemoteAddr());
        post.setLastUpdateTime(now);

        if (sessionUser != null) {
            post.setAuthorId(sessionUser.getId());
            post.setAuthorName(sessionUser.getNickname());
        }

        postService.replyTopic(post);
        return new RedirectResolution(TopicAction.class).addParameter("tid", post.getTopicId());
    }

    public Resolution qutoeForm(){
        post=postService.getPost(qutoeId);
        return getBBSResolution("postForm");
    }

    public Resolution editPostForm(){
        post=postService.getPost(pid);
        return getBBSResolution("postForm");
    }

    public Resolution editPost(){
        Post tempPost=postService.getPost(post.getId());
        tempPost.setContent(post.getContent());
        tempPost.setTitle(post.getTitle());
        tempPost.setLastUpdateTime(new Date());
        tempPost.setIp(context.getRequest().getRemoteAddr());
        tempPost.setUpdateCount(tempPost.getUpdateCount()+1);
        postService.updatePost(tempPost);
        return new RedirectResolution(TopicAction.class).addParameter("tid",tempPost.getTopicId());
    }


}
